package prj.carmap.mobile.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * 商家信息
 *
 */
public class SellerInfo implements Serializable {
	private static final long serialVersionUID = -2203464001595258032L;

	public String sellerId;
	public String sellerName;
	public String sellerType;
	public String telephone;
	public double longitude;
	public double latitude;
	public String address;
	public String sellerPic;
	public int distance;
	public String description;
	public ArrayList<String> sellerPics = new ArrayList<String>();
	public List<ServiceItem> serviceItems = new ArrayList<ServiceItem>();
	public String superSellerType;

	public static SellerInfo fromJson(JSONObject json) {
		SellerInfo info = new SellerInfo();
		try
		{
			info.sellerId = json.getString("sellerId");
			info.sellerName = json.getString("sellerName");
			info.address = json.getString("address");
			info.telephone = json.getString("telephone");
			info.sellerPic = json.getString("sellerPic");
			info.distance = json.getInt("distance");
			info.latitude = json.getDouble("latitude");
			info.longitude = json.getDouble("longitude");
			info.sellerType = json.getString("sellerType");
			info.description = json.getString("description");
			info.superSellerType = json.getString("superSellerType");
			JSONArray ja = json.getJSONArray("sellerPics");
			for(int i = 0; i < ja.length(); i++){
				info.sellerPics.add(ja.getString(i));
			}
			if (json.has("serviceItems"))
			{
				JSONArray jsonAry = new JSONArray();
				try
				{
					jsonAry = json.getJSONArray("serviceItems");
				}
				catch (Exception e)
				{
					jsonAry = new JSONArray("[]");
				}

				for (int i = 0; i < jsonAry.length(); i++)
				{
					ServiceItem item = new ServiceItem();
					item.serviceItemName = jsonAry.getJSONObject(i).getString("serviceItemName");
					item.servicePrice = jsonAry.getJSONObject(i).getInt("servicePrice");
					item.serviceItemDesc = jsonAry.getJSONObject(i).getString("serviceItemDesc");
					info.serviceItems.add(item);
				}
			}
		}
		catch (JSONException e) {
			e.printStackTrace();
		}

		return info;
	}
}
